package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.platform.ai.control.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.di.component.DaggerUserComponent;
import me.jessyan.mvparms.demo.di.module.UserModule;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.entity.Device;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import me.jessyan.mvparms.demo.mvp.presenter.UserPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.LightMainAdapter;
import me.jessyan.mvparms.demo.mvp.ui.holder.ItemClickListener;




@SuppressLint("ValidFragment")
public class AirFragment extends BaseFragment<UserPresenter> implements UserContract.View,SwipeRefreshLayout.OnRefreshListener, SeekBar.OnSeekBarChangeListener {


    @BindView(R.id.rv_model)
    RecyclerView mModel;

    @BindView(R.id.seekbar_def)
    SeekBar mTemBar;

    @BindView(R.id.tv_tem)
    TextView mTem;

    @BindView(R.id.tv_low)
    TextView mLow;

    @BindView(R.id.tv_middle)
    TextView mMiddle;

    @BindView(R.id.tv_high)
    TextView mHigh;

    @BindView(R.id.tv_auto)
    TextView mAuto;

    @BindView(R.id.iv_switch)
    ImageView mSwitch;

    @BindView(R.id.panel)
    View mPanel;

    LightMainAdapter airModelAdapter;
    List<Device> mDevices = new ArrayList<>();
    int tem = 28;
    boolean switchAir = true;
    public static AirFragment newInstance() {
        AirFragment fragment = new AirFragment();
        return fragment;
    }

    public AirFragment(){

    }

    @Override
    public void onResume() {
        super.onResume();
        if(getUserVisibleHint()){

        }
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .userModule(new UserModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_air, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {


        initRecycleView();

    }



    private void initRecycleView() {
        airModelAdapter = new LightMainAdapter(mDevices, new ItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                switch (position) {
                    case 0:
                        mDevices.get(0).setIcon(R.mipmap.icon_cold_on);
                        mDevices.get(0).setState(1);
                        mDevices.get(1).setIcon(R.mipmap.icon_wind_off);
                        mDevices.get(1).setState(0);
                        mDevices.get(2).setIcon(R.mipmap.icon_wet_off);
                        mDevices.get(2).setState(0);
                        mDevices.get(3).setIcon(R.mipmap.icon_hot_off);
                        mDevices.get(3).setState(0);
                        break;
                    case 1:
                        mDevices.get(0).setIcon(R.mipmap.icon_cold_off);
                        mDevices.get(0).setState(0);
                        mDevices.get(1).setIcon(R.mipmap.icon_wind_on);
                        mDevices.get(1).setState(1);
                        mDevices.get(2).setIcon(R.mipmap.icon_wet_off);
                        mDevices.get(2).setState(0);
                        mDevices.get(3).setIcon(R.mipmap.icon_hot_off);
                        mDevices.get(3).setState(0);
                        break;
                    case 2:
                        mDevices.get(0).setIcon(R.mipmap.icon_cold_off);
                        mDevices.get(0).setState(0);
                        mDevices.get(1).setIcon(R.mipmap.icon_wind_off);
                        mDevices.get(1).setState(0);
                        mDevices.get(2).setIcon(R.mipmap.icon_wet_on);
                        mDevices.get(2).setState(1);
                        mDevices.get(3).setIcon(R.mipmap.icon_hot_off);
                        mDevices.get(3).setState(0);
                        break;
                    case 3:
                        mDevices.get(0).setIcon(R.mipmap.icon_cold_off);
                        mDevices.get(0).setState(0);
                        mDevices.get(1).setIcon(R.mipmap.icon_wind_off);
                        mDevices.get(1).setState(1);
                        mDevices.get(2).setIcon(R.mipmap.icon_wet_off);
                        mDevices.get(2).setState(0);
                        mDevices.get(3).setIcon(R.mipmap.icon_hot_on);
                        mDevices.get(3).setState(1);
                        break;
                }
                airModelAdapter.notifyDataSetChanged();
            }
        },1);
        mModel.setAdapter(airModelAdapter);
        ArmsUtils.configRecycleView(mModel, new GridLayoutManager(getContext(),4));

        mTem.setText(String.valueOf(tem));
        mTemBar.setOnSeekBarChangeListener(this);

        mLow.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                mLow.setBackgroundResource(R.mipmap.icon_lev_tab);
                mLow.setTextColor(Color.WHITE);
                mMiddle.setBackground(null);
                mMiddle.setTextColor(Color.BLACK);
                mHigh.setBackground(null);
                mHigh.setTextColor(Color.BLACK);
                mAuto.setBackground(null);
                mAuto.setTextColor(Color.BLACK);
            }
        });
        mMiddle.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                mLow.setBackground(null);
                mLow.setTextColor(Color.BLACK);
                mMiddle.setBackgroundResource(R.mipmap.icon_lev_tab);
                mMiddle.setTextColor(Color.WHITE);
                mHigh.setBackground(null);
                mHigh.setTextColor(Color.BLACK);
                mAuto.setBackground(null);
                mAuto.setTextColor(Color.BLACK);
            }
        });
        mHigh.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                mLow.setBackground(null);
                mLow.setTextColor(Color.BLACK);
                mMiddle.setBackground(null);
                mMiddle.setTextColor(Color.BLACK);
                mHigh.setBackgroundResource(R.mipmap.icon_lev_tab);
                mHigh.setTextColor(Color.WHITE);
                mAuto.setBackground(null);
                mAuto.setTextColor(Color.BLACK);
            }
        });
        mAuto.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                mLow.setBackground(null);
                mLow.setTextColor(Color.BLACK);
                mMiddle.setBackground(null);
                mMiddle.setTextColor(Color.BLACK);
                mHigh.setBackground(null);
                mHigh.setTextColor(Color.BLACK);
                mAuto.setBackgroundResource(R.mipmap.icon_lev_tab);
                mAuto.setTextColor(Color.WHITE);
            }
        });

        mDevices.clear();
        mDevices.add(new Device("制冷",R.mipmap.icon_cold_off,0));
        mDevices.add(new Device("送风",R.mipmap.icon_wind_off,0));
        mDevices.add(new Device("除湿",R.mipmap.icon_wet_on,1));
        mDevices.add(new Device("制热",R.mipmap.icon_hot_off,0));
        airModelAdapter.notifyDataSetChanged();
    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        if (!message.isEmpty()) {

        }
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {

    }

    @Override
    public void killMyself() {

    }


    @Override
    public void setAdapter(DefaultAdapter adapter) {

    }

    @Override
    public void loginReturn(User user) {

    }

    @Override
    public void logout() {

    }


    public void uploadResult(String string) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(progress < 0){
            tem = 16;
            mTemBar.setProgress(0);
            showMessage("空调已调节至最低温度");
        }else if(progress > 20){
            tem = 40;
            mTemBar.setProgress(20);
            showMessage("空调已调节至最高温度");
        }else{
            tem = progress+16;
            mTem.setText(String.valueOf(tem));
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @OnClick(R.id.iv_add)
    public void addTem(View v){
        if(tem >= 36){
            showMessage("空调已调节至最高温度");
        }else {
            tem++;
            mTem.setText(String.valueOf(tem));
            mTemBar.setProgress(tem-16);
        }
    }

    @OnClick(R.id.iv_sub)
    public void subTem(View v){
        if(tem <= 16){
            showMessage("空调已调节至最低温度");
        }else {
            tem--;
            mTem.setText(String.valueOf(tem));
            mTemBar.setProgress(tem-16);
        }

    }


    @OnClick(R.id.iv_switch)
    public void switchAir(View v){
        if(switchAir){
            mSwitch.setImageResource(R.mipmap.switch_off);
            mPanel.setVisibility(View.GONE);
            switchAir = false;
        }else{
            mSwitch.setImageResource(R.mipmap.switch_on);
            mPanel.setVisibility(View.VISIBLE);
            switchAir = true;
        }
    }
}
